<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #canvas {
      box-shadow: 2px 4px 6px #eee;
    }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
</body>
<script src="../script/utils.js"></script>
<script src="../components/Ball.js"></script> 
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')
  
  const W = canvas.width = 800
  const H = canvas.height = 600
  
  let mouse = C.getOffset(canvas)
  let dx = 0, dy = 0

  let isMouseMove = false
  let vx = C.rp([-10, 10]), vy = -10
  let g = 0.2
  let bounce = -0.7

  let startX = 0, startY = 0

  const ball = new Ball({
    x: W / 2,
    y: H / 2,
    r: 60
  }).render(ctx)

  canvas.addEventListener('mousedown', function (e) {
    e.preventDefault()
    if (ball.isPoint(mouse)) {
      isMouseMove = true
      dx = mouse.x - ball.x
      dy = mouse.y - ball.y
      startX = ball.x
      startY = ball.y
      canvas.addEventListener('mousemove', moveBallFn)
      canvas.addEventListener('mouseup', upBallFn)
    }
  })

  function moveBallFn() {
    ball.x = mouse.x - dx
    ball.y = mouse.y - dy
  }
  function upBallFn() {
    isMouseMove = false
    canvas.removeEventListener('mousemove', moveBallFn)
    canvas.removeEventListener('mouseup', upBallFn)
  }

  function bounceMove() {
    vy += g
    ball.x += vx
    ball.y += vy

    if (ball.x + ball.r >= W) {
      ball.x = W - ball.r
      vx *= bounce
    } else if (ball.x - ball.r <= 0) {
      ball.x = ball.r
      vx *= bounce
    }
    if (ball.y + ball.r >= H) {
      ball.y = H - ball.r
      vy *= bounce
    } else if (ball.y - ball.r <= 0) {
      ball.y = ball.r
      vy *= bounce
    }
  }

  function setSpeed() {
    vx = ball.x - startX
    vy = ball.y - startY
    startX = ball.x
    startY = ball.y
  }

  (function move() {
    window.requestAnimationFrame(move)
    ctx.clearRect(0, 0, W, H)

    if (!isMouseMove) {
      bounceMove()
    } else {
      setSpeed()
    }

    ball.render(ctx)
  })()
</script>
</html>